﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Components_Report_TransferProduct : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                gv.Visible = false;
                // int ShopID = Session["ShopID"] == null ? 0 : int.Parse(Session["ShopID"].ToString());
                //Load tinh thanh
                // int ProvinceID = Session["ProvinceID"] == null ? 0 : int.Parse(Session["ProvinceID"].ToString());
                int ShopID = 0;
                int ProvinceID = 0;
                int isProvinceAll = (ProvinceID == 0 ? 1 : 0);
                Utils.loadLocation(cboProvinceFrom, 0, isProvinceAll, ProvinceID);
                Utils.loadLocation(cboProvinceTo, 0, isProvinceAll, ProvinceID);
                //load Shop
                int isShopAll = (ShopID == 0 ? 1 : 0);
                Utils.loadShopByProvinceToCombo(cboShopFrom, isShopAll, ProvinceID, ShopID, "");
                Utils.loadShopByProvinceToCombo(cboShopTo, isShopAll, ProvinceID, ShopID, "");
                Utils.loadProductToComboSearch(cboProduct, 1, "R");
                //  Utils.loadShopByProvinceToCombo(cboShopTo, isShopAll, ProvinceID, ShopID, "");
            }
            catch (Exception ex)
            {
                ControlUtil.LogError("----------------------------");
                ControlUtil.LogError("TransferProduct-LoadForm");
                ControlUtil.LogError(ex.ToString());
                ControlUtil.LogError("----------------------------");
            }
        }
    }

    protected void cboProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        int ProvinceID = int.Parse(this.cboProvinceFrom.SelectedValue == "" ? "0" : this.cboProvinceFrom.SelectedValue);
        Utils.loadShopByProvinceToCombo(cboShopFrom, 1, ProvinceID, 0, "");
    }
    protected void cboProvinceTo_SelectedIndexChanged(object sender, EventArgs e)
    {
        int ProvinceID = int.Parse(this.cboProvinceTo.SelectedValue == "" ? "0" : this.cboProvinceTo.SelectedValue);
        Utils.loadShopByProvinceToCombo(cboShopTo, 1, ProvinceID, 0, "");
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        gv.Visible = true;

        DataTable dt = LoadData();
        gv.DataSource = dt;
        gv.DataBind();
    }
    protected DataTable LoadData()
    {
        DataTable tblResult = new DataTable();
        try
        {
            DateTime FromDate;
            if (dtpFromDate.SelectedDate == null)
            {
                FromDate = DateTime.Parse("2014-01-01");
            }
            else
            {
                FromDate = dtpFromDate.SelectedDate.Value;
            }
            DateTime ToDate;
            if (dtpToDate.SelectedDate == null)
            {
                ToDate = DateTime.Parse("2099-01-01");
            }
            else
            {
                ToDate = dtpToDate.SelectedDate.Value;
            }
            int ProvinceIDFrom = int.Parse(this.cboProvinceFrom.SelectedValue == "" ? "0" : this.cboProvinceFrom.SelectedValue);
            int ShopIDFrom = int.Parse(this.cboShopFrom.SelectedValue);
            int ProvinceIDTo = int.Parse(this.cboProvinceTo.SelectedValue == "" ? "0" : this.cboProvinceTo.SelectedValue);
            int ShopIDTo = int.Parse(this.cboShopTo.SelectedValue);
            int ProductID = int.Parse(this.cboProduct.SelectedValue == "" ? "0" : this.cboProduct.SelectedValue);
            DBDataContext db = LINQUtil.GetDBWriteOnly();
            tblResult = db.Report_TransferProduct(ProvinceIDFrom, ShopIDFrom, ProvinceIDTo, ShopIDTo, ProductID, FromDate, ToDate).ToList().ToDataTable();
        }
        catch (Exception ex)
        {
            ControlUtil.LogError("----------------------------");
            ControlUtil.LogError("TransferProduct-LoadData");
            ControlUtil.LogError(ex.ToString());
            ControlUtil.LogError("----------------------------");
        }
        return tblResult;
    }

    protected void btnExportExcel_Click(object sender, EventArgs e)
    {
        ExportExcel();
    }

    protected void ExportExcel()
    {
        DataTable tblresult = LoadData();
        if (tblresult.Rows.Count > 0)
        {
            ExcelUtil.ExportDataTableToExcel2007FromRadGrid(tblresult, gv, "luanchuyen");
        }
        else
        {
            JavascriptUtil.LoadMessage(Page, false, "Không có dữ liệu xuất Excel !!!");

        }
    }
}